<?php

namespace App\Service\Message\v1_2_7;

use App\Constants\ErrorCode;
use App\Model\CallComment;
use App\Model\VideoCall;
use App\Model\VoiceCall;
use App\Service\User\UserBaseService;

/**
 * 提交通话评价
 */
class CallCommentService extends UserBaseService
{
    protected function RoVerifyList()
    {
        return [
            'channel_id' => 'required|string',
            'comment_level' => 'required|int',
            'comment_tag' => '|string',
            'comment_desc' => '|string',
        ];
    }

    public function handle()
    {
        $userId = $this->getUserId();
        //频道ID
        $channelId = $this->request->input('channel_id', '');
        //通话类型
        $callType = $this->request->input('call_type', 0);
        $callCls = $callType == VoiceCall::CALL_TYPE ? VoiceCall::class : VideoCall::class;
        $callInfo = $callCls::getInfoByChannel($channelId);
        if (!$callInfo || !in_array($userId, [$callInfo->chat_user_id, $callInfo->user_id])) {
            log_info("==wrong_call==" . json_encode($callInfo));
            return $this->setError(ErrorCode::PARAMETER_ERROR, '该话单信息错误，请咨询客服查询');
        }
        //提交信息
        $level = $this->request->input('comment_level', 2);
        $tags = $this->request->input('comment_tag', '');
        $desc = $this->request->input('comment_desc', '');
        //提交评价
        CallComment::addRecord(
            $userId,
            $callInfo,
            $level,
            $tags,
            $desc
        );
        return ['result' => 1];
    }
}
